import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
import { login, getUserInfo, notices } from '@/api/api.js'

const store = new Vuex.Store({
  state: {
    user: {
      id: 92,
      phone: '19000000001',
      password: '',
      nickName: '测试123',
      inviterId: '2',
      invitePhone: '15000000001',
      inviteCode: '',
      state: 0,
      level: 0,
      pids: '2',
      award: '0.00',
      codeNum: 0,
      channel: 'starsmoon',
      bossId: 2,
      createdAt: '2023-04-28 12:02:33',
      updatedAt: '2023-06-05 21:51:20',
      aliPayment: null,
      wechatPayment: null,
      bankPayment: null,
      phoneCount: 1,
      levelName: '普通'
    },
    token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcHBfaWQiOjkyLCJhdWQiOiIiLCJleHAiOjE3MTE4OTQ0OTYsImlhdCI6MTY4NTk3NDQ5NiwiaXNzIjoiIiwianRpIjoiOTM4YjAzNDE5MGJjMDZlNjM4MGE4ZGYxYzMwMjgzNTUiLCJuYmYiOjE2ODU5NzQ0OTYsInN1YiI6IiJ9.E5W0DFZGViPuWsAAmHgl2YqaJ_CUUwoFUyNVBQ9K22s',
    showAction: false,
    noticeList: [],
    bankBranck: ''
  },
  mutations: {
    SAVE_USER(state, user) {
      state.user = user
    },
    SET_TOKEN(state, token) {
      state.token = token
    },
    CHANGE_SHOWACTION(state, showAction) {
      state.showAction = showAction
    },
    SAVE_NOTICE(state, noticeList) {
      state.noticeList = noticeList
    },
    SAVE_BRANCH(state, bankBranck) {
      state.bankBranck = bankBranck
    }
  },
  actions: {
    login({ commit }, params) {
      login(params).then((res) => {
        if (res.code == 10000) {
          commit('SET_TOKEN', res.data.token)
          commit('SAVE_USER', res.data.user)
          uni.switchTab({
            url: '/pages/index/index'
          })
        } else {
          uni.showToast({
            title: res.message
          })
        }
      })
    },
    userInfo({ commit }) {
      getUserInfo().then((res) => {
        if (res.code === 10000) {
          commit('SAVE_USER', res.data)
        } else {
          uni.showToast({
            title: res.message
          })
        }
      })
    },
    changeShowAction({ commit }, value) {
      commit('CHANGE_SHOWACTION', value)
    },
    setNotice({ commit }) {
      notices().then((res) => {
        if (res.code === 10000) {
          commit('SAVE_NOTICE', res.data)
        }
      })
    },
    setBankBranch({ commit }) {
      commit('SAVE_NOTICE', bankPayment)
    }
  }
})

export default store
